﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
using System.Data;

public class BasePage : Page
{

    private static string[] aspNetFormElements = new string[] 

  { 

    "__EVENTTARGET",

    "__EVENTARGUMENT",

    "__VIEWSTATE",

    "__EVENTVALIDATION",

    "__VIEWSTATEENCRYPTED",

  };



    protected override void Render(HtmlTextWriter writer)
    {

        StringWriter stringWriter = new StringWriter();

        HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

        base.Render(htmlWriter);

        string html = stringWriter.ToString();

        int formStart = html.IndexOf("<form");

        int endForm = -1;

        if (formStart >= 0)

            endForm = html.IndexOf(">", formStart);



        if (endForm >= 0)
        {

            StringBuilder viewStateBuilder = new StringBuilder();

            foreach (string element in aspNetFormElements)
            {

                int startPoint = html.IndexOf("<input type=\"hidden\" name=\"" + element + "\"");

                if (startPoint >= 0 && startPoint > endForm)
                {

                    int endPoint = html.IndexOf("/>", startPoint);

                    if (endPoint >= 0)
                    {

                        endPoint += 2;

                        string viewStateInput = html.Substring(startPoint, endPoint - startPoint);

                        html = html.Remove(startPoint, endPoint - startPoint);

                        viewStateBuilder.Append(viewStateInput).Append("\r\n");

                    }

                }

            }



            if (viewStateBuilder.Length > 0)
            {

                viewStateBuilder.Insert(0, "\r\n");

                html = html.Insert(endForm + 1, viewStateBuilder.ToString());

            }

        }



        writer.Write(html);

    }

}

public partial class MasterPage2 : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Load_ParentProduct();
        }
        this.RefreshShoppingCartInfo();
    }
    public void RefreshShoppingCartInfo()
    {
        ShoppingCart Cart = (ShoppingCart)Session["ShoppingCart"];
        lblAmount.Text = Cart.Total.ToString("#.#");
        lblItems.Text = Cart.Count.ToString();
    }

    protected void Load_ParentProduct()
    {
        //----hien thi danh muc cap 1----
        SqlConnection mycon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        mycon.Open();
        string sql = "select * from LoaiTaiLieu where IDLoaiTLCha=0"; //---menu cap 1 la menu co ParentId=0
        SqlCommand myCommand = new SqlCommand(sql, mycon);
        SqlDataAdapter myAdap = new SqlDataAdapter(myCommand);
        DataSet ds = new DataSet();
        myAdap.Fill(ds);
        rpListParentProduct.DataSource = ds.Tables[0];
        rpListParentProduct.DataBind();
        myCommand.Dispose();
        mycon.Close();
    }

    protected void rpListParentProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //---tuong ung voi mot san pham A duoc dua ra, hien thi danh sach san pham con cua A
        HiddenField hrId = (HiddenField)e.Item.FindControl("hrID"); //--- day la Id cua loai san pham A---
        Repeater rpChildrent = (Repeater)e.Item.FindControl("rpListChildProduct");
        if ((hrId != null) && (rpChildrent != null))
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            conn.Open();
            string sql = "select * from LoaiTaiLieu where IDLoaiTLCha=" + hrId.Value.ToString(); //---menu con cua A co ParentID la Id cua A
            //string sql = "select * from ProductGroup where ParentId=0";
            SqlCommand myCommand = new SqlCommand(sql, conn);
            SqlDataAdapter myAdap = new SqlDataAdapter(myCommand);
            DataSet ds = new DataSet();
            myAdap.Fill(ds);
            rpChildrent.DataSource = ds.Tables[0];
            rpChildrent.DataBind();
            myCommand.Dispose();
            conn.Close();
        }
    }
}
